<?php
/* ===========================
 * * 解析主类类
 * @ by 子不语 爱不语CMS
 * @ QQ:76992327
 * @ Create:2014.10.9
  ============================ */
if (!defined('KKINC')) exit('Request Error!');
class parse {
    function str_array($str) {
        $arr = array();
        if (!is_array($str)) parse_str(trim($str), $arr);
        //获取数量
        if ($arr['pg']) {
            $arr['start'] = ($arr['pg'] - 1) * $arr['show'];
        }
        if ($arr['start'] && $arr['show']) {
            $num = ' limit ' . (int) $arr['start'] . ',' . (int) $arr['show'];
        } else if ($arr['show']) {
            $num = ' limit ' . (int) $arr['show'];
        }
        //排序
        /* if ($arr['orderby']) {
          $orderby = $arr['orderby'];
          } else {
          $orderby = 'sortrank asc,id asc';
          } */
        if ($arr['field']) {
            $arr['field'] = ',' . $arr['field'];
        }
        $arr['num'] = $num;
        $arr['where'] = 'where 1 ';
        return $arr;
    }

    /* =========================
     *  调用栏目名称
     * @param     int  $tid  栏目ID
     *  return string
      ========================= */
    protected function get_arctype($tid = 0) {
        global $base;
        $get_type =ApplicationHelper::instance('appConfig','getArctype',array($tid));
        return $get_type;
    }

    /* =========================
     *  调用栏目名称
     * @param     int  $tid  栏目ID
     *  return string
      ========================= */
    protected function get_typename($tid) {
        $get_type = $this->get_arctype($tid);
        return $get_type['typename'];
    }

    /* =========================
     *  遍历子栏目
     * @param     int  $tid  栏目ID
     * @param     array  $arctype  栏目数组
     *  return string
      ========================= */
    function get_arctype_son($id, $arctype) {
        $arr = array();
        foreach ($arctype as $v) {
            if ($v['fid'] == $id) {
                array_push($arr, $v);
                $temp = $this->get_arctype_son($v['id'], $arctype);
                if (is_array($temp)) {
                    foreach ($temp as $value) {
                        array_push($arr, $value);
                    }
                }
            }
        }
        if (!empty($arr)) return $arr;
        return false;
    }
    /* ===================
      循环获取子栏目id供sql
     * @ int
     * @ int //判断是否循环获取
      =================== */
    function get_arctype_son_sql($id, $xh = 0) {
        $arctype = $this->get_arctype();
        $newarr = $this->get_arctype_son($id, $arctype);
        $result = '';        
        foreach ($newarr as $key => $value) {
            $result.=$value['id'] . ',';
        }
        return $result.$id;
    }
    /* ===================
      图片图片的链接
     * @ string 图片路径
     * @ string 
      =================== */
    function replace_pic($pic) {
        if($second_dir){
            return '/'.$second_dir.$pic;
        }
        return $pic;
    }
}